package com.example.work_sqlite_note;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button add;
    private ListView listView;
    private List<Temp> temps = new ArrayList<>();
    private static final String TITLE = "title";
    private static final String CONTENT = "content";
    private static final String LIST = "list";

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        add = findViewById(R.id.add);
        listView = findViewById(R.id.listview);
        Connector.getDatabase();
        Log.d(TAG, "onClick: 创建数据库");
        dataContent();
        setAddlistener();
//        TempAdapter tempAdapter = new TempAdapter(MainActivity.this,R.layout.notes_item,temps);
//        ListView listView = findViewById(R.id.listview);
//        listView.setAdapter(tempAdapter);

    }

    private void dataContent() {

        temps = DataSupport.findAll(Temp.class);
        for (Temp t : temps) {
            Log.d(TAG, "dataContent: " + t.getTitle());
        }
        TempAdapter tempAdapter = new TempAdapter(MainActivity.this, R.layout.notes_item, temps);
        ListView listView = findViewById(R.id.listview);
        listView.setAdapter(tempAdapter);
    }

    private void setAddlistener() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, Describe.class);
                intent.putExtra("add","add");
                startActivityForResult(intent, 1);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String title_value = "";
                String content_value = "";
                List<Temp> temps2 = DataSupport.findAll(Temp.class);


                title_value = temps2.get((int) id).getTitle();
                content_value = temps2.get((int) id).getContent();


                Intent intent = new Intent(MainActivity.this, Describe.class);
                intent.putExtra(TITLE, title_value);
                intent.putExtra(CONTENT, content_value);
                intent.putExtra(LIST, "list");
                startActivityForResult(intent, 1);
                Log.d(TAG, "onItemClick: " + id);

            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {
                Log.d(TAG, "onItemLongClick: 弹窗");
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("警告");
                builder.setMessage("确认删除？");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        List<Temp> tt = DataSupport.findAll(Temp.class);

                        DataSupport.delete(Temp.class, tt.get((int) id).getId());


                        Log.d(TAG, "onClick: 删除" + id + 1);
                        dataContent();

                    }
                });
                builder.show();
                return true;
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult: 返回了数据");
        if (resultCode == RESULT_OK) {
            dataContent();
        }
    }

}
